package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import beans.Solicituddcc;
import beans.Usuario;
import beans.listas.SolicitudInforme;
import conexion.BDConexion;
import dao.interfaces.SolicituddccDAO;

public class MySqlSolicituddccDAO implements SolicituddccDAO {

	@Override
	public int registrarSolicitud(Solicituddcc bean) {
		int salida = -1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "INSERT INTO bd_inictel_dcc.solicituddc VALUES (?,?,?,?,?,?,?,?,?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, bean.getNumero());
			pst.setString(2, bean.getFecha());
			pst.setString(3, bean.getCliente());
			pst.setString(4, bean.getCurso());
			pst.setInt(5, bean.getParticipantes());
			pst.setString(6, bean.getHini());
			pst.setString(7, bean.getHfin());
			pst.setString(8, bean.getFrecuencia());
			pst.setInt(9, bean.getEstado());
			salida = pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		return salida;
	}

	@Override
	public String obtenerCodigo() {
		String codi = null;
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "SELECT concat(identificador,numerador) as codigo FROM bd_inictel_dcc.correlativo WHERE identificador = 'SOL';";
			pst = cn.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				codi = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return codi;
	}

	@Override
	public List<SolicitudInforme> ListarSolInformeDisp() {
		List<SolicitudInforme> lista = new ArrayList<SolicitudInforme>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {

			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.Listar_Solicitudes_Informe();";
			pst = cn.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				SolicitudInforme s = new SolicitudInforme(rs.getString(1),
						rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getInt(5), rs.getInt(6), rs.getInt(7),
						rs.getString(8));
				lista.add(s);
			}
		} catch (Exception e) {
				e.printStackTrace();
		} finally {
			try {
				if (cn != null) {
					cn.close();
				}
				if (pst != null) {
					pst.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

}
